package com.csx;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author 陈胤训
 * @date 2019/6/18 18:36
 * Utils: Intellij Idea
 * Description: 二倍均值算法
 */
public class DoubleMeanValue {
    /**
     * 发红包算法，金额参数以分为单位
     * @param totalAmount 红包金额 以分为单位
     * @param totalPeopleNum 人数
     * @return
     */
    public static List<Integer> divideRedPackage(Integer totalAmount,
                                                 Integer totalPeopleNum) {

        List<Integer> amountList = new ArrayList<Integer>();

        Integer restAmount = totalAmount;

        Integer restPeopleNum = totalPeopleNum;

        Random random = new Random();

        for (int i = 0; i < totalPeopleNum - 1; i++) {

            // 随机范围：[1，剩余人均金额的两倍)，左闭右开

            int amount = random.nextInt(restAmount / restPeopleNum * 2 - 1) + 1;
            restAmount -= amount;
            restPeopleNum--;
            amountList.add(amount);
        }
        amountList.add(restAmount);

        return amountList;
    }

    public static void main(String[] args) {

        List<Integer> amountList = divideRedPackage(5, 3);

        for (Integer amount : amountList) {

            System.out.println("抢到金额：" + new BigDecimal(amount).divide(new BigDecimal(100)));
        }
    }

}
